package com.sanjiahantang.youjiaclient.module.common.presenters;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.google.gson.Gson;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.beans.ScreenAdBean;
import com.sanjiahantang.youjiaclient.module.common.models.IMainModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.MainModel;
import com.sanjiahantang.youjiaclient.module.common.views.SplashView;
import com.sanjiahantang.youjiaclient.utils.AdvertiseUtil;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

/**
 * Created by zy on 2017/10/17.
 */

public class SplashPresenter extends BaseMvpPresenter<SplashView> {

    private IMainModel mainModel;
    public SplashPresenter(){
        mainModel = new MainModel();
    }

    public void loadScreenAdvisement(Context context) {
        mainModel.loadScreenAdvisement(context,new CallBack<ScreenAdBean>(){

            @Override
            public void onSuccess(ScreenAdBean response, Context context, Object tag) {
                saveScreenAd(context,response);
            }

            @Override
            public void onFailure(String message) {
            }
        });
    }

    private void saveScreenAd(final Context context, final ScreenAdBean response) {
        final Gson gson = new Gson();
        if(response!=null&&response.getSsaId() != 0){
            ScreenAdBean spBean = gson.fromJson(SPUtils.get(context,SPConfig.SCREEN_ADVERTISEMENT,"{}").toString(),ScreenAdBean.class);
            if (spBean.getSsaId() != response.getSsaId()){
                if (response.getUrl() != null){
                    response.setUrl(FileUtils.rePath(response.getUrl()));
                    AdvertiseUtil.getAdverImgFromServer(response, new BaseView.OnLoadListener(){

                        @Override
                        public void onLoad(Object content) {
                            SPUtils.put(context,SPConfig.SCREEN_ADVERTISEMENT,gson.toJson(content));
                        }
                    });
                }
            }

        }

    }
}
